package dbcloud;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import org.zeromq.ZMQ;


public class ZMQConnFactory {
	
	private static final ZMQ.Context context = ZMQ.context(1);
	private HashMap<Grid, ZMQ.Socket> conn_table = new HashMap<Grid, ZMQ.Socket>();
	private ArrayList<ZMQ.Socket> all_conn = new ArrayList<ZMQ.Socket>();
	private ZMQ.Socket default_conn;
	public int REQUEST_TIMEOUT = 5000; // msecs, (> 1000!)
	
	private static int DEFAULT_CONN_NUM = 65535;
	
	public static final int default_grid_zoom = 11;
	
	public void Init(String conf_path/*"D:\\commserver\\db"*/)
	{
		conn_table.clear();
		all_conn.clear();
		
		HashMap<Integer, ZMQ.Socket> tmp_map = new HashMap<Integer, ZMQ.Socket>();
		
		File file = new File(conf_path + "HASHCONNECTION");
		BufferedReader reader = null;
		try{
			reader = new BufferedReader(new FileReader(file));
			
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				if(tempString.length() > 0)
				{
					if(tempString.charAt(0) == '#')
					{
						continue;
					}
					String test[] = tempString.split("\\s+");
        		  
					if(test.length < 2)
					{
						continue;
					}
					int conn_num = Integer.parseInt(test[0]);
					String conn_str = test[1];   
        		   
					ZMQ.Socket socket = context.socket(ZMQ.REQ);
					socket.setReceiveTimeOut(REQUEST_TIMEOUT);
					socket.connect(conn_str);
        		   
					if(conn_num == DEFAULT_CONN_NUM)
					{
						default_conn = socket;
					}
					else
					{
						tmp_map.put(conn_num, socket);
					}
					all_conn.add(socket);
				}
			}
			reader.close();
		} catch (IOException e) {
            e.printStackTrace();
            return;
        }
		
		file = new File(conf_path + "HASHVALUE");
		try{
			reader = new BufferedReader(new FileReader(file));
			
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {

				if(tempString.length() > 0)
				{
					if(tempString.charAt(0) == '#')
					{
						continue;
					}
					String test[] = tempString.split("\\s+");
        		  
					if(test.length < 3)
					{
						continue;
					}
					int x = Integer.parseInt(test[0]);
					int y = Integer.parseInt(test[1]);
					int num = Integer.parseInt(test[2]);
					
					conn_table.put(new Grid(x, y, default_grid_zoom), tmp_map.get(num));
					
				}
			}
			reader.close();
		} catch (IOException e) {
            e.printStackTrace();
            return;
        }
		
	}
	
	public void Release()
	{
		conn_table.clear();
		all_conn.clear();
	}
	
	public ZMQ.Socket getConnection(double lng, double lat)
	{
		
		Grid grid= new Grid(GridFunc.getTileNumberX(default_grid_zoom, lng),
							GridFunc.getTileNumberY(default_grid_zoom, lat),
							default_grid_zoom);
		
		ZMQ.Socket s = conn_table.get(grid);
		
		if(s == null)
			return default_conn;
		else
			return s;
	}
	
	public ZMQ.Socket getConnection(int x, int y, int zoom)
	{
		if(zoom < default_grid_zoom)
			return null;
		Grid grid= new Grid(x, y, zoom);	
		
		ZMQ.Socket s = conn_table.get(grid.toHighGrid(default_grid_zoom));
		
		if(s == null)
			return default_conn;
		else
			return s;
	}
	
	public ArrayList<ZMQ.Socket> getAllConnection()
	{
		return all_conn;
	}
}
